package com.nf.handler;

import com.nf.MiniResultHandler;
import com.nf.MiniRowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MiniBeanHandler<T> implements MiniResultHandler<T> {

    private final Class<? extends T>type;
    //调用行转换器进行转换
    private final MiniRowProcessor convert;

    public MiniBeanHandler(Class<? extends T> type) {
        this(type,Constants.DEFAULT_ROW_PROCESSOR);
    }

    public MiniBeanHandler(Class<? extends T> type, MiniRowProcessor convert) {
        this.type = type;
        this.convert = convert;
    }

    @Override
    public T handle(ResultSet rs) throws SQLException {
        return rs.next()?convert.toBean(rs,type):null;
    }
}
